// pages/user/help/question/question.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cardImgPathFront: '',
    cardImgPathReverse: '',
    cardImgPathOther: '',
    auth_disabled: false,
    auth_msg: '确认提交',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('onload');
    var _this = this;
    var userInformation = app.globalData.userInformation;
    this.setData({
      realName: userInformation.realName,
      gender: userInformation.gender ? userInformation.gender : null,
      cardId: userInformation.cardId,
      cardImgPathFront: userInformation.cardImgPathFront,
      cardImgPathReverse: userInformation.cardImgPathReverse,
      cardImgPathOther: userInformation.cardImgPathOther
    });
    wx.request({
      url: app.globalData.apiDomain + 'crowdsource/wechat/checkAuditStatus',
      header: { 'Content-Type': 'application/json', 'token': app.globalData.token },
      success: function (res) {
        console.log(res);
        if (res.data.status == 'ok') {

          // 提交审核
          if (res.data.object.auditstatus == 1) {
            // 身份认证正在审核
            _this.setData({
              auth_disabled: true,
              auth_msg: '等待审核'
            });

          } else if (res.data.object.auditstatus == 2) {
            // 审核成功
            _this.setData({
              auth_disabled: true,
              auth_msg: '审核通过'
            });

          } else if (res.data.object.auditstatus == 3) {
            // 审核失败
            _this.setData({
              auth_disabled: false,
              auth_msg: '重新提交'
            });
            wx.showModal({
              content: res.data.object.auditdesc ? '身份认证审核失败 : ' + res.data.object.auditdesc : '身份认证审核失败',
              showCancel: false
            })
          } else {
            _this.setData({
              auth_disabled: false,
              auth_msg: '确认提交'
            });
          }

        } else {
          app.responseCheck(_this, res.data, '/pages/user/auth/auth')
        }
      },
      fail: function (e) {
        wx.showModal({
          content: '网络异常,请重试',
          showCancel: false
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },
  photoTypeOne: function(){
    var _this = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: ['camera'],
      success: function (res) {
        var tempFilePaths = res.tempFilePaths
        console.log(tempFilePaths);
        wx.showLoading({
          mask: true
        })
        wx.uploadFile({
          url: app.globalData.apiDomain + 'crowdsource/wechat/uploadCardImg',
          header: { 
            'Content-Type': 'multipart/form-data',
            'token': app.globalData.token 
          },
          filePath: tempFilePaths[0],
          name: 'file',
          formData: { 
            "projectTypeId": 5,
          },
          success: function (res) {
            var image = JSON.parse(res.data);
            if(image.status == 'ok') {
              _this.setData({
                cardImgPathFront: image.object
              });
            }else{
              app.responseCheck(_this, image, '/pages/user/auth/auth');
            }
          },
          fail: function (res) {
            wx.showModal({
              content: '网络异常,请重试',
              showCancel: false
            })
          },
          complete: function() {
            wx.hideLoading()
          }
        })
      }
    })
  },
  photoTypeTwo: function () {
    var _this = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: ['camera'],
      success: function (res) {
        var tempFilePaths = res.tempFilePaths
        console.log(tempFilePaths);
        wx.showLoading({
          mask: true
        })
        wx.uploadFile({
          url: app.globalData.apiDomain + 'crowdsource/wechat/uploadCardImg',
          header: {
            'Content-Type': 'multipart/form-data',
            'token': app.globalData.token
          },
          filePath: tempFilePaths[0],
          name: 'file',
          formData: {
            "projectTypeId": 5,
          },
          success: function (res) {
            var image = JSON.parse(res.data);
            if (image.status == 'ok') {
              _this.setData({
                cardImgPathReverse: image.object
              });
            }else{
              app.responseCheck(_this, image, '/pages/user/auth/auth');
            }
          },
          fail: function (res) {
            wx.showModal({
              content: '网络异常,请重试',
              showCancel: false
            })
          },
          complete: function () {
            wx.hideLoading()
          }
        })
      }
    })
  },
  photoTypeThree: function () {
    var _this = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: ['camera'],
      success: function (res) {
        var tempFilePaths = res.tempFilePaths
        console.log(tempFilePaths);
        wx.showLoading({
          mask: true
        })
        wx.uploadFile({
          url: app.globalData.apiDomain + 'crowdsource/wechat/uploadCardImg',
          header: {
            'Content-Type': 'multipart/form-data',
            'token': app.globalData.token
          },
          filePath: tempFilePaths[0],
          name: 'file',
          formData: {
            "projectTypeId": 5,
          },
          success: function (res) {
            var image = JSON.parse(res.data);
            if (image.status == 'ok') {
              _this.setData({
                cardImgPathOther: image.object
              });
            }else{
              app.responseCheck(_this, image, '/pages/user/auth/auth')
            }
          },
          fail: function (res) {
            wx.showModal({
              content: '网络异常,请重试',
              showCancel: false
            })
          },
          complete: function () {
            wx.hideLoading()
          }
        })
      }
    })
  },
  formSubmit: function(e) {
    var _this = this;
    
    var username = e.detail.value.username
    var sex = e.detail.value.sex
    var identity = e.detail.value.identity
    var cardImgPathFront = this.data.cardImgPathFront
    var cardImgPathReverse = this.data.cardImgPathReverse
    var cardImgPathOther = this.data.cardImgPathOther
    
    console.log(e);
    console.log(sex);

    if(!username) {
      wx.showToast({
        title: '请输入姓名',
        icon: 'success',
        duration: 2000
      })
      return;
    }

    // var pattern = /^[\u4e00-\u9fa5]{2,4}$/;
    // if (!pattern.test(username)) {
    //   wx.showToast({
    //     title: '真实姓名有误',
    //     icon: 'success',
    //     duration: 2000
    //   })
    //   return;
    // }  

    if(sex != 17 && sex != 18) {
      wx.showToast({
        title: '请输入性别',
        icon: 'success',
        duration: 2000
      })
      return;
    }
    
    if (!identity) {
      wx.showToast({
        title: '请输入身份证',
        icon: 'success',
        duration: 2000
      })
      return;
    }

    // var identifyNo = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
    // if (!identifyNo.test(identity)) {
    //   wx.showToast({
    //     title: '身份证有误',
    //     icon: 'success',
    //     duration: 2000
    //   })
    //   return;
    // }  

    if (!cardImgPathFront || !cardImgPathReverse || !cardImgPathOther) {
      wx.showToast({
        title: '请上传照片',
        icon: 'success',
        duration: 2000
      })
      return;
    }
    this.setData({
      auth_disabled: true
    });


    wx.showModal({
      content: '当姓名与支付宝账户不匹配可能造成提现失败，是否继续',
      confirmText: '是',
      cancelText: '否',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.apiDomain + 'crowdsource/wechat/userComfirmAudit',
            method: 'POST',
            data: {
              "realName": username,
              'gender': sex,
              'cardId': identity,
              "cardImgPathFront": cardImgPathFront,
              "cardImgPathReverse": cardImgPathReverse,
              "cardImgPathOther": cardImgPathOther
            },
            header: {
              'content-type': 'application/json',
              'token': app.globalData.token
            },
            success: function (res) {
              console.log(res);
              if (res.data.status == 'ok') {

                app.globalData.userInformation.realName = username
                app.globalData.userInformation.gender = sex
                app.globalData.userInformation.cardId = identity
                app.globalData.userInformation.cardImgPathFront = cardImgPathFront
                app.globalData.userInformation.cardImgPathReverse = cardImgPathReverse
                app.globalData.userInformation.cardImgPathOther = cardImgPathOther

                wx.showToast({
                  title: 'Yes! 提交成功',
                  icon: 'success',
                  duration: 2000
                })
                setTimeout(function () {
                  _this.setData({
                    auth_disabled: false
                  });
                  wx.navigateBack({
                    delta: 1
                  })
                }, 2000);
              } else {
                _this.setData({
                  auth_disabled: false
                });
                app.responseCheck(_this, res.data, '/pages/user/auth/auth')
              }
            },
            fail: function (res) {
              _this.setData({
                auth_disabled: false
              });
              wx.showModal({
                content: '网络异常,请重试',
                showCancel: false
              })
            },
          })
        } else if (res.cancel) {
          _this.setData({
            auth_disabled: false
          });
        }
      }
    })
  }
  

})